package com.xyh.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyh.common.util.ResultCode;
import com.xyh.common.util.ResultVO;
import com.xyh.common.validate.groups.Create;
import com.xyh.common.validate.groups.Update;
import com.xyh.system.mapper.UserRoleMapper;
import com.xyh.system.model.User;
import com.xyh.system.service.IRoleService;
import com.xyh.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private UserRoleMapper userRole;

    /**
     * url:user/index
     * 跳转到用户管理列表界面
     *
     * @return
     */
    @GetMapping("/index")
    public String toIndex() {
        System.out.println("=====UserController.toIndex=====");
        return "user/user-list";
    }

    /**
     * url:user/list
     * 多条件查询并分页获取所有用户列表
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVO getList(Integer page, Integer limit, User userQuery) {
        System.out.println("====多条件查询并分页获取所有用户列表UserController.getList====");
        System.out.println("页数page = " + page);
        System.out.println("每页限制条数limit = " + limit);
        System.out.println("查询条件userQuery = " + userQuery);

        IPage<User> userPage = userService.selectAllWithDept(page, limit, userQuery);

        return ResultVO.success(userPage);
    }

    /**
     * 跳转至新增界面
     *
     * @return
     */
    @GetMapping
    public String toAdd(Model model) {
        //读取角色
        model.addAttribute("roles", roleService.list());
        return "user/user-add";
    }

    /**
     * 跳转至更新界面(和新增页面一致)
     */
    @GetMapping("/{userId}")
    public String toUpdate(@PathVariable("userId") Integer userId, Model model) {

        model.addAttribute("roleIds", userService.selectRoleIdsByUserId(userId));
        model.addAttribute("user", userService.getById(userId));
        model.addAttribute("roles", roleService.list());
        return "user/user-add";
    }

    /**
     * 执行新增操作 前台是POST提交
     *
     * @param user
     * @return
     * @Validated(Create.class)验证分组
     */
    @PostMapping
    @ResponseBody
    public ResultVO add(@Validated(Create.class) User user, @RequestParam(value = "role[]", required = false) Integer[] roleIds) {
        System.out.println("========UserController.add========");
        return ResultVO.success(userService.add(user, roleIds));
    }

    /**
     * 执行修改操作 前台是PUT提交
     */
    @PutMapping
    @ResponseBody
    public ResultVO update(@Validated(Update.class) User user, @RequestParam(value = "role[]", required = false) Integer[] roleIds) {
        System.out.println("========UserController.update========");
        System.out.println("user = " + user);
        userService.update(user, roleIds);
        return ResultVO.success();
    }

    /**
     * 执行删除操作
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    @ResponseBody
    public ResultVO delete(@PathVariable("userId") Integer userId) {
        boolean b = userService.removeById(userId);//其实执行的是更新操作，还需在字段上加注解@TableLogic

        if (b) {
            userRole.deleteUserRoleByUserId(userId);
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 账号状态(正常)点击事件
     *
     * @return
     */
    @PostMapping("/{userId}/enable")
    @ResponseBody
    public ResultVO enableStatus(@PathVariable("userId") Integer userId) {

        boolean flag = userService.enableStatus(userId);
        if (flag) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 账号状态(锁定)点击事件
     *
     * @return
     */
    @PostMapping("/{userId}/disable")
    @ResponseBody
    public ResultVO disableStatus(@PathVariable("userId") Integer userId) {

        boolean flag = userService.disableStatus(userId);
        if (flag) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

//    /**
//     * 跳转至注册界面
//     *
//     * @return
//     */
//    @GetMapping("/register")
//    public String toRegister() {
//        return "register";
//    }

}
